package com.example.back.service.impl;

import cn.hutool.core.date.DateUtil;
import com.example.back.dto.TransactionExportXls;
import com.example.back.dto.req.TransactionListReq;
import com.example.back.dto.resp.TransactionListResp;
import com.example.back.mapper.TransactionMapper;
import com.example.back.service.TransactionService;
import com.example.back.utils.ExcelUtils;
import com.example.common.enums.AType;
import com.example.common.enums.PayType;
import com.example.common.enums.SystemStatus;
import com.example.common.support.ApiException;
import com.example.common.utils.CheckUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author wxy
 * @Date 2021/1/14 13:10
 * @Version 1.0
 */
@Service
public class TransactionServiceImpl implements TransactionService {

    @Resource
    private TransactionMapper transactionMapper;
    @Resource
    private ExcelUtils excelUtils;

    /**
     * 交易记录
     */
    @Override
    public PageInfo<TransactionListResp> list(TransactionListReq req) {

        CheckUtils.checkNull(req, new ApiException(1, "对象不能为空"));

        //分页页码，默认1页
        int pageNumber = null == req.getPage() ? 1 : req.getPage();
        //分页条数，默认10条
        int pageSize = null == req.getLimit() ? 10 : req.getLimit();
        //订单号
        String orderNo = req.getOrderNO();
        //开始时间
        Date startTime = req.getStartTime();
        //结束时间
        Date EndTime = req.getEndTime();
        //订单类型
        int types = req.getTypes();
        //支付方式
        int payType = req.getPayType();
        //交易状态
        int systemStatus = req.getSystemStatus();

        return PageHelper
                .startPage(pageNumber, pageSize)
                .doSelectPageInfo(() -> transactionMapper.findByTransactionLists(orderNo, startTime, EndTime, types, payType, systemStatus));
    }

    /**
     * 导出交易记录
     */
    @Override
    public Object TransactionExportXls(TransactionListReq req) {
        CheckUtils.checkNull(req, new ApiException(1, "对象不能为空"));

        //订单号
        String orderNo = req.getOrderNO();
        //开始时间
        Date startTime = req.getStartTime();
        //结束时间
        Date EndTime = req.getEndTime();
        //订单类型
        int types = req.getTypes();
        //支付方式
        int payType = req.getPayType();
        //交易状态
        int systemStatus = req.getSystemStatus();

        List<TransactionListResp> list = transactionMapper.findByTransactionLists(orderNo, startTime, EndTime, types, payType, systemStatus);
        List<TransactionExportXls> dataList = list.stream().map(m -> {
            TransactionExportXls xls = new TransactionExportXls();
            xls.setOrderNo(m.getOrderNO());
            xls.setTransactionId(m.getTransactionID());
            xls.setNickName(m.getNickName());
            xls.setAskMoney(null == m.getAskMoney() ? new BigDecimal(0).toString() : m.getAskMoney().toString());
            xls.setTypes(AType.valueToDesc(m.getTypes()));
            xls.setPayType(PayType.valueToDesc(m.getPayType()));
            xls.setSystemStatus(SystemStatus.valueToDesc(m.getSystemStatus()));
            xls.setCreateTime(DateUtil.formatDateTime(m.getCreateTime()));
            return xls;
        }).collect(Collectors.toList());

        String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmssS") + "-交易记录";

        try {
            excelUtils.excelExport(TransactionExportXls.class, dataList, fileName);
        } catch (Exception e) {
            throw new ApiException(1, e.getMessage());
        }
        return null;
    }
}
